package util;

import android.webkit.JavascriptInterface;

import com.alibaba.fastjson.JSONArray;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import bean.MP3Info;
import music.Music;

/**
 * Created by Administrator on 2017/3/6.
 */

public class DBUtil {

    private boolean initFlag = false;
    private boolean artistInitFlag = false;
    private boolean albumInitFlag = false;
    private boolean floderInitFlag = false;

    @JavascriptInterface
    public String init(){
        List<MP3Info> list = DataSupport.findAll(MP3Info.class);
        initFlag = true;
        return JSONArray.toJSONString(list);
    }

    @JavascriptInterface
    public String artistInit(){
        List<List<MP3Info>> artistList = new ArrayList<>();
        List<MP3Info> list = new ArrayList<>();
        list.addAll(Music.cacheList);
        while (list.size() > 0){
            List<MP3Info> one = new ArrayList<>();
            one.add(list.get(0));
            list.remove(0);
            for (int i = 0; i <= list.size() - 1; ){
                if (list.get(i).getArtist().equals(one.get(0).getArtist())){
                    one.add(list.get(i));
                    list.remove(i);
                } else  {
                    i++;
                }
            }
            artistList.add(one);
        }
        Music.artistList = artistList;
        artistInitFlag = true;

        return JSONArray.toJSONString(artistList);
    }

    @JavascriptInterface
    public String albumInit(){
        List<List<MP3Info>> albumList = new ArrayList<>();
        List<MP3Info> list = new ArrayList<>();
        list.addAll(Music.cacheList);
        while (list.size() > 0){
            List<MP3Info> one = new ArrayList<>();
            one.add(list.get(0));
            list.remove(0);
            for (int i = 0; i <= list.size() - 1; ){
                if (list.get(i).getAlbum().equals(one.get(0).getArtist())){
                    one.add(list.get(i));
                    list.remove(i);
                }else  {
                    i++;
                }
            }
            albumList.add(one);
        }
        Music.albumList = albumList;
        albumInitFlag = true;

        return JSONArray.toJSONString(albumList);
    }

    @JavascriptInterface
    public String floderInit(){
        List<List<MP3Info>> floderList = new ArrayList<>();
        List<MP3Info> list = new ArrayList<>();
        list.addAll(Music.cacheList);
        while (list.size() > 0){
            List<MP3Info> one = new ArrayList<>();
            one.add(list.get(0));
            list.remove(0);
            for (int i = 0; i <= list.size() - 1; ){
                if (list.get(i).getPath().substring(0, list.get(i).getPath().lastIndexOf('/')).equals(one.get(0).getPath().substring(0, one.get(0).getPath().lastIndexOf('/')))){
                    one.add(list.get(i));
                    list.remove(i);
                }else  {
                    i++;
                }
            }
            floderList.add(one);
        }
        Music.floderList = floderList;
        floderInitFlag = true;

        return JSONArray.toJSONString(floderList);
    }

    @JavascriptInterface
    public boolean isInitFlag() {
        return initFlag;
    }

    @JavascriptInterface
    public boolean isArtistInitFlag() {
        return artistInitFlag;
    }

    @JavascriptInterface
    public boolean isAlbumInitFlag() {
        return albumInitFlag;
    }

    @JavascriptInterface
    public boolean isFloderInitFlag() {
        return floderInitFlag;
    }
}
